// cf-100796-j
// 题意：给定一辆车，n个操作，可以从前门进一个人，后门进一个人，第i个
//       上车的人要下车，选择从前门或者后门人少的下，然后前面的人先从
//       对应的门下去，再从另一个门上去。
//
// 题解：经典splay。提高效率用prev和succ存每个人前驱和后继，然后维护下。
//
// 统计：63ms
// run: $exec < input
// opt: 0
// flag: -g
#include <iostream>
#include <cstdio>
#include <cstring>

int const maxn = 100100;
int n, tot;

namespace splay_tree
{
	struct node
	{
		node() { size = 0; }
		void set_child(node * c, int is_right) { child[is_right] = c; c->parent = this; }
		bool is_right() { return this == parent->child[1]; }

		int size;
		node * child[2], * parent, * succ, * prev;
	} * root, memory[maxn], * nil, * alloc = memory, * first, * last;

	node * node_alloc()
	{
		alloc->child[0] = alloc->child[1] = nil;
		alloc->size = 1;
		return alloc++;
	}

	void update(node * t)
	{
		t->size = t->child[0]->size + t->child[1]->size + 1;
	}

	void rot(node * t)
	{
		node * p = t->parent;
		bool is_right = t->is_right();
		p->parent->set_child(t, p->is_right());
		p->set_child(t->child[!is_right], is_right);
		t->set_child(p, !is_right);
		update(p);
		if (p == root) root = t;
	}

	void splay(node * t, node * parent)
	{
		while (t->parent != parent) {
			if (t->parent->parent == parent)
				rot(t);
			else
				(t->is_right() == t->parent->is_right()) ?
					(rot(t->parent), rot(t)) :
						(rot(t), rot(t));
		}
		update(t);
	}

	void splay(node * t) { splay(t, nil); }

	node * build(int l, int r)
	{
		if (l >= r) return nil;
		int mid = (l + r) / 2;
		node * t = node_alloc();
		t->set_child(build(l, mid), 0);
		t->set_child(build(mid + 1, r), 1);
		update(t);
		return t;
	}

	void init()
	{
		nil = new node;
		root = build(0, 2); root->parent = nil;
		first = memory + 1; last = memory;
		first->prev = last->succ = nil;
		first->succ = last;
		last->prev = first;
	}

	node * select(int k)
	{
		for (node * t = root; ; ) {
			int c = t->child[0]->size;
			if (k == c) return t;
			if (k > c)
				k -= c + 1, t = t->child[1];
			else
				t = t->child[0];
		}
	}

	void insert_last()
	{
		node * t = last->prev;
		node * tr = last;
		node * tmp = node_alloc();

		tmp->prev = t;
		tmp->succ = tr;
		t->succ = tmp;
		tr->prev = tmp;

		splay(t);
		splay(tr, t);

		tmp->set_child(tr->child[0], 0);
		tr->set_child(tmp, 0);
		update(tr);
	}

	void insert_first()
	{
		node * t = first;
		node * tr = first->succ;
		node * tmp = node_alloc();

		tmp->prev = t;
		tmp->succ = tr;
		t->succ = tmp;
		tr->prev = tmp;

		splay(t);
		splay(tr, t);

		tmp->set_child(tr->child[0], 0);
		tr->set_child(tmp, 0);
		update(tr);
	}


	void remove(node * x)
	{
		node * t = x->prev;
		node * tr = x->succ;

		t->succ = tr;
		tr->prev = t;

		splay(t);
		splay(tr, t);

		tr->child[0] = nil;
		update(tr);
		splay(tr);
	}

	void remove_query(int p)
	{
		node * t = memory + 2 + p;
		splay(t);
		int left = t->child[0]->size - 1, right = t->child[1]->size - 1;
		if (left < right) {
			std::printf("%d\n", left);
			splay(first, t);
			node * tl = first->child[1];
			first->child[1] = nil;
			node * tlast = last->prev;

			tlast->succ = first->succ;
			first->succ->prev = tlast;
			last->prev = t->prev;
			t->prev->succ = last;
			t->prev = first;
			first->succ = t;

			splay(tlast);

			tlast->child[1]->set_child(tl, 0);
			update(tlast->child[1]);
			splay(tlast->child[1]);
			remove(t);
		} else {
			std::printf("%d\n", right);
			splay(last, t);
			node * tl = last->child[0];
			last->child[0] = nil;
			node * tfirst = first->succ;

			tfirst->prev = last->prev;
			last->prev->succ = tfirst;
			first->succ = t->succ;
			t->succ->prev = first;
			t->succ = last;
			last->prev = t;

			splay(tfirst);

			tfirst->child[0]->set_child(tl, 1);
			update(tfirst->child[0]);
			splay(tfirst->child[0]);
			remove(t);
		}
	}
};

int main()
{
	std::scanf("%d", &n);
	splay_tree::init();
	char command[2];
	for (int i = 0; i < n; i++) {
		std::scanf("%s", command);

#ifdef __DEBUG
		std::cout << "-----------------------------> " << i << '\n';
		for (int j = 0; j < tot; j++) {
			std::cout << j + 1 << " prev: " << ((splay_tree::memory + 2 + j)->prev - splay_tree::memory) - 1 <<
				"  succ: " << ((splay_tree::memory + 2 + j)->succ - splay_tree::memory) - 1 << '\n';
		}
#endif

		if (command[0] == 'F') {
			splay_tree::insert_last();
			tot++;
			continue;
		}
		if (command[0] == 'B') {
			splay_tree::insert_first();
			tot++;
			continue;
		}
		if (command[0] == 'O') {
			int id; std::scanf("%d", &id);
			splay_tree::remove_query(--id);
			tot--;
			continue;
		}
	}
}

